// let timeUtils = require('./timeUtils')
import timeUtils from './timeUtils'



let getRecord = async function name(account,
  selectLib, i,
  MAX_LIMIT) {
  //不停从数据库拿数据，实现分页查询

  let res = await wx.cloud.database().collection("testRecords").where({
    account,
    selectLib
  }).skip(i * MAX_LIMIT).limit(MAX_LIMIT).orderBy('time', 'desc').get()
  let title = res.data
  let response = new Array()
  //从title中提取考试时间，考试得分和_id
  for (let i = 0; i < title.length; i++) {
    let element = title[i];
    let {
      _id,
      score,
      time
    } = element
    time = await timeUtils.timeFormat(time)
    let data = {
      _id,
      score,
      time
    }
    response.push(data)
  }
  return response
}

let searchTitle = async function (title, selectLib) {
  let res = await wx.cloud.database().collection("shuati").where({
    name: selectLib
  }).get()
  const arr = res.data[0].title
  const pattern = new RegExp(title, 'i');
  console.log(pattern instanceof RegExp)
  //   const result = arr.find(obj=> obj.question.includes(title))
  const result = arr.filter(obj =>
    pattern.test(String(obj.question))
  )
  return result
}


module.exports = {
  getRecord,
  searchTitle
}